namespace SinaFinanceBaseClassLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Shapes;

    public class TimeSharingLine : UserControl
    {
        private int controlZindex = 10;
        private Image coreImage = new Image();
        private TimeSharingCrossLine crossLine = new TimeSharingCrossLine();
        private string currentStockID = string.Empty;
        private List<Point> dataPoints = new List<Point>();
        private List<string> leftYaxisDatalist = new List<string>();
        private List<double> leftYDatalist = new List<double>();
        private Color lineColor = Colors.Gray;
        private List<Line> LineInCanvas = new List<Line>();
        private double lineTickness = 1.0;
        private Canvas parentCanvas;
        private Point pointLeftBottom = new Point();
        private Point pointLeftTop = new Point();
        private List<TimeSharingPoint> pointList = new List<TimeSharingPoint>();
        private Point pointRightBottom = new Point();
        private Point pointRightTop = new Point();
        private List<string> rightYaxisDatalist = new List<string>();
        private List<double> rightYDatalist = new List<double>();
        private List<string> xaxisDatalist = new List<string>();
        private Dictionary<DateTime, double> xAxisMarkPointsDic = new Dictionary<DateTime, double>();
        private double xAxisUnitLen = 0.0;
        private List<DateTime> xDatalist = new List<DateTime>();
        private const int xTimSharingMinuteSub = 30;
        private double yAxisUnitLen = 0.0;
        private Dictionary<double, double> yLeftAxisMarkPointsDic = new Dictionary<double, double>();
        private Dictionary<double, double> yRightAxisMarkPointsDic = new Dictionary<double, double>();

        protected List<TimeSharingPoint> InitCrossingLineTimeSharingPoint(int index)
        {
            List<TimeSharingPoint> list = new List<TimeSharingPoint>();
            if (!index.Equals(-1) && (this.pointList != null))
            {
                if (this.pointList.Count <= index)
                {
                    return list;
                }
                List<TimeSharingPoint> pointList = this.pointList;
                if ((pointList.Count > index) && (pointList[index] != null))
                {
                    TimeSharingPoint point = pointList[index];
                    list.Add(point);
                }
            }
            return list;
        }

        public List<Point> InitIntersectionPoints(Point corePoint, out int index)
        {
            List<Point> list = new List<Point>();
            index = -1;
            bool flag = false;
            if (this.dataPoints.Count >= 1)
            {
                List<Point> dataPoints = this.dataPoints;
                if (dataPoints.Count.Equals(0))
                {
                    return list;
                }
                double num = Math.Abs((double) (dataPoints[0].X - dataPoints[1].X));
                if (!flag)
                {
                    for (int i = 0; i < dataPoints.Count; i++)
                    {
                        Point point = dataPoints[i];
                        if (point.X.Equals(corePoint.X))
                        {
                            flag = true;
                            index = i;
                        }
                        if (((corePoint.X - point.X) < num) && ((corePoint.X - point.X) >= 0.0))
                        {
                            index = i;
                        }
                    }
                }
                if (index > -1)
                {
                    list.Add(dataPoints[index]);
                }
            }
            return list;
        }

        public void RemoveCrossLine()
        {
            this.crossLine.RemoveCrossLine();
        }

        public void RemoveLineObj()
        {
            if (this.parentCanvas != null)
            {
                for (int i = 0; i < this.LineInCanvas.Count; i++)
                {
                    Line line = this.LineInCanvas[i];
                    if (this.parentCanvas.Children.Contains(line))
                    {
                        this.parentCanvas.Children.Remove(line);
                    }
                }
            }
        }

        public void RemoveLineObj(PointTypeEnum pointTypeEnum)
        {
            if (this.parentCanvas != null)
            {
                for (int i = 0; i < this.LineInCanvas.Count; i++)
                {
                    Line line = this.LineInCanvas[i];
                    if (this.parentCanvas.Children.Contains(line) && line.Tag.ToString().Equals(pointTypeEnum.ToString()))
                    {
                        this.parentCanvas.Children.Remove(line);
                    }
                }
            }
        }

        public void RenderCrossLine(Point corePoint)
        {
            int index = -1;
            List<Point> list = this.InitIntersectionPoints(corePoint, out index);
            List<TimeSharingPoint> list2 = this.InitCrossingLineTimeSharingPoint(index);
            this.crossLine.ParentCanvas = this.parentCanvas;
            this.crossLine.PointLeftBottom = this.pointLeftBottom;
            this.crossLine.PointLeftTop = this.pointLeftTop;
            this.crossLine.PointRightTop = this.pointRightTop;
            this.crossLine.IntersectionPoint = list;
            this.crossLine.TimeSharingPoints = list2;
            this.crossLine.CoreImage = this.coreImage;
            this.crossLine.ControlZindex = this.controlZindex;
            this.crossLine.DrawCrossLine(this.currentStockID);
        }

        public void RenderLine(PointTypeEnum pointTypeEnum)
        {
            if (this.parentCanvas != null)
            {
                this.RenderPointPosition(pointTypeEnum);
                if (!this.dataPoints.Count.Equals(0))
                {
                    List<Point> dataPoints = this.dataPoints;
                    for (int i = 0; i < (dataPoints.Count - 1); i++)
                    {
                        Point point = dataPoints[i];
                        Point point2 = dataPoints[(int) (i + 1)];
                        Line line = new Line {
                            X1 = point.X,
                            X2 = point2.X,
                            Y1 = point.Y,
                            Y2 = point2.Y,
                            Tag = pointTypeEnum
                        };
                        line.Stroke = new SolidColorBrush(this.lineColor);
                        line.StrokeThickness = this.lineTickness;
                        line.SetValue(Canvas.ZIndexProperty, this.controlZindex);
                        this.parentCanvas.Children.Add(line);
                        this.LineInCanvas.Add(line);
                    }
                }
            }
        }

        protected void RenderPointPosition(PointTypeEnum pointTypeEnum)
        {
            this.TransformAxisPointPosition(this.xaxisDatalist, XorYAxisEnum.xAxis);
            this.TransformAxisPointPosition(this.leftYaxisDatalist, XorYAxisEnum.leftYaxis);
            this.TransformAxisPointPosition(this.rightYaxisDatalist, XorYAxisEnum.rightYaxis);
            List<Point> list = new List<Point>();
            for (int i = 0; i < this.pointList.Count; i++)
            {
                TimeSharingPoint point = this.pointList[i];
                DateTime time = Convert.ToDateTime(point.XInfo);
                double num2 = Convert.ToDouble(point.YInfo.Replace("%", "").Replace("+", ""));
                List<double> leftYDatalist = new List<double>();
                double x = 0.0;
                double y = 0.0;
                bool flag = false;
                bool flag2 = false;
                int num5 = 0;
                while (num5 < (this.xDatalist.Count - 1))
                {
                    if (this.xDatalist[num5].Equals(DateTime.Parse("11:30")))
                    {
                        this.xDatalist[num5]= DateTime.Parse("13:00");
                    }
                    if (time == this.xDatalist[num5])
                    {
                        x = this.pointLeftTop.X + (this.xAxisUnitLen * num5);
                        flag = true;
                        break;
                    }
                    if ((time > this.xDatalist[num5]) && (time < this.xDatalist[(int) (num5 + 1)]))
                    {
                        int num6 = ((TimeSpan) (time - this.xDatalist[num5])).Minutes;
                        x = this.pointLeftTop.X + (this.xAxisUnitLen * (num5 + (((double) num6) / 30.0)));
                        flag = true;
                        break;
                    }
                    if (time == this.xDatalist[(int) (num5 + 1)])
                    {
                        x = this.pointLeftTop.X + (this.xAxisUnitLen * (num5 + 1));
                        flag = true;
                        break;
                    }
                    num5++;
                }
                leftYDatalist = this.leftYDatalist;
                if (pointTypeEnum.Equals(PointTypeEnum.Percent))
                {
                    num2 *= 100.0;
                }
                for (num5 = 0; num5 < (leftYDatalist.Count - 1); num5++)
                {
                    if ((num2 <= leftYDatalist[num5]) && (num2 >= leftYDatalist[(int) (num5 + 1)]))
                    {
                        double num7 = leftYDatalist[num5] - num2;
                        y = this.pointLeftTop.Y + (this.yAxisUnitLen * (num5 + Math.Abs((double) (num7 / (leftYDatalist[(int) (num5 + 1)] - leftYDatalist[num5])))));
                        flag2 = true;
                        break;
                    }
                }
                Point point2 = new Point(x, y);
                if (flag && flag2)
                {
                    list.Add(point2);
                }
            }
            this.dataPoints = list;
        }

        protected void TransformAxisPointPosition(List<string> orignalDatalist, XorYAxisEnum xORyAxisEnum)
        {
            int num;
            bool flag = false;
            for (num = 0; num < (orignalDatalist.Count - 1); num++)
            {
                if (orignalDatalist[num].Equals(orignalDatalist[(int) (num + 1)]))
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                this.xAxisMarkPointsDic.Clear();
                this.yLeftAxisMarkPointsDic.Clear();
                this.yRightAxisMarkPointsDic.Clear();
                for (num = 0; num < orignalDatalist.Count; num++)
                {
                    string str = orignalDatalist[num];
                    double num2 = 0.0;
                    double num3 = 0.0;
                    if (xORyAxisEnum.Equals(XorYAxisEnum.xAxis))
                    {
                        this.xDatalist.Add(Convert.ToDateTime(str));
                        num3 = this.pointLeftTop.X + (this.xAxisUnitLen * num);
                        if (!this.xAxisMarkPointsDic.ContainsKey(Convert.ToDateTime(str)))
                        {
                            this.xAxisMarkPointsDic.Add(Convert.ToDateTime(str), num3);
                        }
                    }
                    else if (xORyAxisEnum.Equals(XorYAxisEnum.leftYaxis))
                    {
                        num2 = Convert.ToDouble(str.Replace("%", ""));
                        this.leftYDatalist.Add(num2);
                        num3 = this.pointLeftTop.Y + (this.yAxisUnitLen * num);
                        this.yLeftAxisMarkPointsDic.Add(num2, num3);
                    }
                    else
                    {
                        num2 = Convert.ToDouble(str.Replace("%", ""));
                        this.rightYDatalist.Add(num2);
                        num3 = this.pointLeftTop.Y + (this.yAxisUnitLen * num);
                        this.yRightAxisMarkPointsDic.Add(num2, num3);
                    }
                }
            }
        }

        public int ControlZindex
        {
            get
            {
                return this.controlZindex;
            }
            set
            {
                this.controlZindex = value;
            }
        }

        public Image CoreImage
        {
            get
            {
                return this.coreImage;
            }
            set
            {
                this.coreImage = value;
            }
        }

        public string CurrentStockID
        {
            get
            {
                return this.currentStockID;
            }
            set
            {
                this.currentStockID = value;
            }
        }

        public List<string> LeftYaxisDatalist
        {
            get
            {
                return this.leftYaxisDatalist;
            }
            set
            {
                this.leftYaxisDatalist = value;
            }
        }

        public Color LineColor
        {
            get
            {
                return this.lineColor;
            }
            set
            {
                this.lineColor = value;
            }
        }

        public double LineTickness
        {
            get
            {
                return this.lineTickness;
            }
            set
            {
                this.lineTickness = value;
            }
        }

        public Canvas ParentCanvas
        {
            get
            {
                return this.parentCanvas;
            }
            set
            {
                this.parentCanvas = value;
            }
        }

        public Point PointLeftBottom
        {
            get
            {
                return this.pointLeftBottom;
            }
            set
            {
                this.pointLeftBottom = value;
            }
        }

        public Point PointLeftTop
        {
            get
            {
                return this.pointLeftTop;
            }
            set
            {
                this.pointLeftTop = value;
            }
        }

        public List<TimeSharingPoint> PointList
        {
            get
            {
                return this.pointList;
            }
            set
            {
                this.pointList = value;
            }
        }

        public Point PointRightBottom
        {
            get
            {
                return this.pointRightBottom;
            }
            set
            {
                this.pointRightBottom = value;
            }
        }

        public Point PointRightTop
        {
            get
            {
                return this.pointRightTop;
            }
            set
            {
                this.pointRightTop = value;
            }
        }

        public List<string> RightYaxisDatalist
        {
            get
            {
                return this.rightYaxisDatalist;
            }
            set
            {
                this.rightYaxisDatalist = value;
            }
        }

        public List<string> XaxisDatalist
        {
            get
            {
                return this.xaxisDatalist;
            }
            set
            {
                this.xaxisDatalist = value;
            }
        }

        public double XAxisUnitLen
        {
            get
            {
                return this.xAxisUnitLen;
            }
            set
            {
                this.xAxisUnitLen = value;
            }
        }

        public double YAxisUnitLen
        {
            get
            {
                return this.yAxisUnitLen;
            }
            set
            {
                this.yAxisUnitLen = value;
            }
        }
    }
}
